<?php
// Prevent direct access to this file
defined('IN_APPS') or die('No direct access!');

/**
 * Debug function
 */
function pr($str, $array = false)
{
	echo '<pre>';
	if ($array) {
		print_r($str);
	}
	else {
		var_dump($str);
	}
	echo '</pre>';
}


/**
 * Generate site URL WITHOUT index.php, querystring and trailing slash
 * 
 * ie:		http://www.example.com  
 */ 
function base_url()
{
	// set base_url so we can use it in our apps
	$dir = str_replace('/index.php', '', $_SERVER['PHP_SELF']); 
	
	$base_url = 'http';
	
//	if ($_SERVER['HTTPS'] == 'on') {
//		$base_url .= 's';
//	}
	
	$base_url .= '://';
	
	if ($_SERVER['SERVER_PORT'] != '80') {
		$base_url .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$dir;
	} 
	else {
		$base_url .= $_SERVER['SERVER_NAME'].$dir;
	}
	
	return $base_url;
}


/**
 * Generate site URL including index.php, but without querystring
 * 
 * ie:		http://www.example.com/index.php  
 */ 
function site_url()
{
	// set base_url so we can use it in our apps
	$dir = $_SERVER['PHP_SELF']; 
	
	$base_url = 'http';
	
//	if ($_SERVER['HTTPS'] == 'on') {
//		$base_url .= 's';
//	}
	
	$base_url .= '://';
	
	if ($_SERVER['SERVER_PORT'] != '80') {
		$base_url .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$dir;
	} 
	else {
		$base_url .= $_SERVER['SERVER_NAME'].$dir;
	}
	
	return $base_url;
}


/**
 * Redirect user to specific location, and with some optional message
 *
 * @param	array	$location		Array containing controller name and controller action
 * @param	string	$message		If we set this message, controller will automatically parse it and show to view
 * @param	string	$type			We can set either 'notice' or 'error' for our message
 */    
function redirect($location, $message = null, $type = 'notice')
{
	// get the controller if defined, if not just set it to fontpage
	$controller = isset($location['controller']) ? $location['controller'] : 'frontpage';
	
	// get the controller action if defined, if not just set it to index
	$action = isset($location['action']) ? $location['action'] : 'action';
	
	unset($location['controller']);
	unset($location['action']);
		
	$param = '';
	foreach ($location as $key => $value) {
		$param .= '&' . $key . '=' . $value;
	}
	
	$uri = base_url() . '/index.php?controller=' . $controller . '&action=' . $action . $param;
	
	// do we need to pass the message?
	if (!empty($message)) {
		$uri .= '&message=' . $message . '&type=' . $type;
	}
	
	header("Location: ".$uri);
	exit;
}


/**
 * Get file extension
 * 
 * @param	string		$str	Filename
 * @return	string		$ext	File extension  
 */
function getExtension($str) 
{
	$i = strrpos($str,".");
	
	if (!$i) { 
		return ''; 
	}
	
	$l = strlen($str) - $i;
    $ext = substr($str, $i + 1, $l);
    
	return $ext;
 }
 
 
 function getReCaptcha()
 {
	 global $config;
	 
	 require_once LIBRARY_PATH . DS . 'recaptcha' . DS . 'recaptchalib.php';
		
	// get re-captcha public key from config
	return recaptcha_get_html($config['recaptcha_public_key']);
 }
 
function checkReCaptcha($challenge, $response) 
{
	global $config;
	
	require_once LIBRARY_PATH . DS . 'recaptcha' . DS . 'recaptchalib.php';
	
	return recaptcha_check_answer ($config['recaptcha_private_key'],
                                $_SERVER["REMOTE_ADDR"],
                                $challenge,
                                $response); 
}


/**
 * Create encrypted password
 */
function encryptPassword($password) 
{
	global $config;
	
	$str = sha1( $config['encryption_key'] . $password . $config['encryption_key'] );
	
	return $str;
}



/**
 * Remove Invisible Characters
 *
 * This prevents sandwiching null characters
 * between ascii characters, like Java\0script.
 *
 * @access	public
 * @param	string
 * @return	string
 */
function remove_invisible_characters($str, $url_encoded = TRUE)
{
	$non_displayables = array();
	
	// every control character except newline (dec 10)
	// carriage return (dec 13), and horizontal tab (dec 09)
	
	if ($url_encoded)
	{
		$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
		$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
	}
	
	$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127

	do
	{
		$str = preg_replace($non_displayables, '', $str, -1, $count);
	}
	while ($count);

	return $str;
}



/**
 * Validate email address
 *
 * @access	public
 * @return	bool
 */

function valid_email($address)
{
	return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
}
